package com.lxmes.device.utils;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class GetDate {
/**
 * 当前时间所在一周的周一和周日时间
 * @return
 */
/*public static Map<String,String> getWeekDate() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    Calendar cal = Calendar.getInstance();
    cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
    if(dayWeek==1){
        dayWeek = 8;
    }
    System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期

    cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
    Date mondayDate = cal.getTime();
    String weekBegin = sdf.format(mondayDate);
    System.out.println("所在周星期一的日期：" + weekBegin);


    cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
    Date sundayDate = cal.getTime();
    String weekEnd = sdf.format(sundayDate);
    System.out.println("所在周星期日的日期：" + weekEnd);

    map.put("mondayDate", weekBegin);
    map.put("sundayDate", weekEnd);
    return map;
}*/
public  static  String getWeekFirstDay(){
    LocalDate today = LocalDate.now();
    LocalDate monday = today.with(TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY));

    return  monday.toString();
}
    public static  String getWeekLastDay(){
        LocalDate today = LocalDate.now();
        LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY ));
        return  sunday.toString();

    }
    /**
     * 当月第一天
     *
     * @return
     */
    public static String getMonthFirstDay() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = df.format(gcLast.getTime());
        StringBuffer str = new StringBuffer().append(day_first).append(
                " 00:00:00");
        return str.toString();

    }

    /**
     * 当月最后一天
     *
     * @return
     */
    public static String getMonthLastDay() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();
        String s = df.format(theDate);
        StringBuffer str = new StringBuffer().append(s).append(" 23:59:59");
        return str.toString();

    }

}

